﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Expenses.DAL.Interfaces;
using Expenses.DAL.Repositories;
using Expenses.Domain.Entities;
using Expenses.Models;
//using Spring.Context.Support;
//using Spring.Objects.Factory.Attributes;

namespace Expenses.Controllers
{
    public class InvoiceController : Controller 
    {
        private IInvoiceRepository repository;
        private IExpenseRepository expenseRepository;

        public IInvoiceRepository Repository
        {
            get { return repository; }
            set { repository = value; }
        }

        public IExpenseRepository ExpenseRepository
        {
            get { return expenseRepository; }
            set { expenseRepository = value; }
        }


        public ViewResult Index()
        {
            return View(Repository.GetAll().OrderBy(i => i.Expense.Name));
        }


        public ViewResult Edit(Guid id)
        {
            return View(new InvoiceModel(Repository.GetById(id)));

        }

        [HttpPost]
        public ActionResult Edit( Invoice invoice )
        {
            if ( ModelState.IsValid )
            {
                Repository.Update( invoice);
                
                return RedirectToAction("Index");
            }
            return View( invoice );
        }

        [HttpPost]
        public ActionResult Search(string searchString)
        {
            var invoices = Repository.GetAll();


            return View("Index", invoices.Where(e => e.Expense.Name.Contains("Agua")));

        }

        public ActionResult Details(Guid id)
        {

            return View();

        }

        public ActionResult Delete(Guid id)
        {

            return View();

        }

        public ActionResult Create()
        {
            var im = new InvoiceModel();
            im.Expenses = ExpenseRepository.GetAll();

            return View(im);
        }

        [HttpPost]
        public ActionResult Create(InvoiceModel invoiceModel)
        {
            var invoice = invoiceModel.CreateInvoiceFromModel();
            
            Repository.Add( invoice );

            return View("Index", Repository.GetAll());
        }
    }
}
